const app = getApp()
var BLELocker = require("./BLELocker.js");
const lbbService = require('../pages/service/service.js');

//处理设备连接状态变化回调
function handleStatus(status, data, that, _callback, failBack) {
    console.log('handleStatus------------->', status, failBack)
    wx.hideLoading()
    if (status == BLELocker.Status.ble01Data) {
      that.setData({
        loading_show: true,
        loading_desc: '数据读取中',
      });
    } else if (status == BLELocker.Status.bleScanStart) {
      that.setData({
        loading_show: true,
        loading_desc: '设备连接中',
      });
    } else if (status == BLELocker.Status.appVersionNotSupported) {
      that.setData({
        loading_show: false
      })
      wx.showToast({
        title: '微信版本太低',
        image:'/images/gantan.png'
      })
      if (failBack) {
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
        }, 1000)
      }
    } else if (status == BLELocker.Status.bleIsClosed || status == BLELocker.Status.adaperOpenFail) {
      that.setData({
        loading_show: false,
        modal: {
          modal_show: true,
          desc: '请打开蓝牙!或关闭后重新打开',
          confirm: true
        }
      })
      if (failBack) {
        that.setData({
          retryBtnShow:true
        })
      }
    } else if (status == BLELocker.Status.bleNotSupportWXService || status == BLELocker.Status.bleNotSupportWXCharacteristic) {
      that.setData({
        loading_show: false
      })
      wx.showToast({
        title: '系统版本低',
        image: '/images/gantan.png'
      })
      if (failBack) {
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
        }, 1000)
      }
    } else if (status == BLELocker.Status.bleReceiveAuthData) {
      that.setData({
        loading_desc: "数据读取中",
        loading_show: true
      });
    } else if (status == BLELocker.Status.bleConnectTimeout) {
      //1.关闭正在连接的提示对话框 2.弹出提示用户连接设备超时,重新扫码
      console.log('监听到bleConnectTimeout', that.data.curIsDirectCon)
      if (that.data.curIsDirectCon){
        console.log('安卓直连 start');
        that.data.directFirstIsFail = false; //安卓蓝牙连接第一次是否失败
        BLELocker.directConnect('DK' + that.data.macId, function (status, data) {
          handleStatus(status, data, that, _callback, true)
        }, 0, false, null, null, that);
      }else {
        that.setData({
          loading_show: false,
          modal: {
            desc: '连接超时请重试',
            retryConnect: true,
            modal_show: true
          }
        })
        if (failBack) {
          that.setData({
            retryBtnShow: true
          })
        }
      }
    } else if (status == BLELocker.Status.bleClosed) {
      that.setData({
        loading_show: false,
        modal: {
          modal_show: true,
          desc: '蓝牙已关闭,重新打开后重试',
          confirm: true
        }
      })
      if (failBack) {
        that.setData({
          retryBtnShow: true
        })
      }
    } else if (status == BLELocker.Status.bleOpened) {
      if (failBack) {
        that.setData({
          retryBtnShow: true
        })
      }

    } else if (status == BLELocker.Status.bleSend85Timeout) {
      that.setData({
        loading_show: false,
        modal: {
          modal_show: true,
          desc: '开锁超时,重试',
          confirm: true
        }
      })
    } else if (status == BLELocker.Status.bleSend95Timeout) {
      that.setData({
        loading_show: false,
        modal: {
          modal_show: true,
          desc: '读取失败,请重试',
          retry95: true
        }
      })
      if (failBack) {
        that.setData({
          retryBtnShow: true
        })
      }

    } else if (status == BLELocker.Status.ble85Data || status == BLELocker.Status.ble88Data) {
      if (data && data.isNew === 1) {
        if (data.backSeq == 1) {

        } else if (data.backSeq == 2) {
          BLELocker.disconnect('statusHandler95')
        }
      }

      if (data && data.isNew === 0) {
        BLELocker.disconnect('statusHandler99')
      } else {
        
      }

      if (data == null) {
        that.setData({
          loading_show: false,
          openning_show: false,
          modal: {
            modal_show: true,
            desc: '开锁失败,扫码归还后重试',
            comfirm: true
          },
        })
        
      } else {
        console.log('UI层收到85数据', data);

        that.setData({
          loading_show: false,
          openning_show:false
        });
      }

    } else if (status == BLELocker.Status.ble95Data) {
      //数据读取中
      that.setData({
        loading_show: true,
        loading_desc: '数据读取中',
      });
      if (data == null) {
        that.setData({
          loading_show: false,
          modal: {
            modal_show: true,
            desc: '连接失败,扫码重试',
            confirm: true
          }
        })
        
      } else {
        if (data.motorStat == 1) {
          that.setData({
            loading_show: false,
            openning_show: true,
          });
          return;
        }

        data.hardwareVersion = getApp().globalData.hardwareVersion
        data.softwareVersion = getApp().globalData.softwareVersion
    
        that.data.data_95 = data;
        that.setData({
          loading_show: false
        });
        console.log("处理95指令接收的数据", data,_callback);
        
        lbbService.record95data(data);

        if (_callback){
          _callback();
        }
      }
    } else if (status == BLELocker.Status.ble98Data) {
      
    }

}

module.exports.handleStatus = handleStatus